import './index.css';
import 'utils/js/px2rem.pc';
import LoadingPC from 'components/loading-pc';
import ImageBG from 'components/image-bg';
import VideoBG from 'components/video-bg';
import QRCode from 'components/qrcode';
import { apiRoot, wsRoot } from 'utils/js/config';
import MsgSlider from 'utils/js/jquery.msgSlider.1';
import { getParamFromSearch } from 'utils/js/tools';
import AIChatSocket from 'utils/js/aichat-socket';
import $ from 'jquery';

var queryParam = getParamFromSearch();
var loading = new LoadingPC();
var imageBG = new ImageBG('.bgImg', [
  require('asset/1.jpg'),
  require('asset/2.jpg'),
  require('asset/3.jpg'),
]);
var videoBG = new VideoBG('.bgVideo', [
  require('asset/520.webm'),
  require('asset/1314.webm'),
]);
var qrcode = new QRCode('.erweimaWall');


loading.load({
  images: [
    require('asset/1.jpg'),
    require('asset/2.jpg'),
    require('asset/3.jpg'),
  ],
  videos: [
    [require('asset/520.webm'), 2],
    [require('asset/1314.webm'), 2],
  ],
  promises: [
    new Promise(function (resolve, reject) {
      new AIChatSocket({
        url: `${wsRoot}ws/chat/${queryParam.rid}/pc`,
        onopen: function () {
          resolve(this);
        },
        onerror: reject,
        onclose: reject,
      });
    }),
    new Promise(function (resolve, reject) {
      $.ajax({
        type: 'get',
        url: `${apiRoot}v1/record/${queryParam.rid}`,
        dataType: 'json',
        success: function (res) {
          if (res.code == 200) {
            resolve(res.data)
          } else {
            reject(res.error)
          }
        },
        error: reject,
      });
    }),
  ],
}).then(function (obj) {
  var arr = obj.promises;
  var ws = arr[0]; // WebSocket实例
  var chatData = arr[1]; // 初始聊天消息

  // 初始化聊天墙
  var msgSlider = new MsgSlider('#msgbox', chatData);


  // socket消息处理
  ws.opt.onmessage = function (d, e) {
    switch (d.type) {
      case 'chat':
        msgSlider.insert(d.data);
        break;
      default:
        console.log(d);
    }
  };
}).catch(function (e) {
  console.error(e);
});
